package com.arges.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author fanweitao
 * @date 2022/12/13
 */
public class AppUrl {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String url = "http://172.16.3.162:18502/account/direct-access?code=ARGES&client_id=appCode" +
                "&timestamp=1647256924679&sign=sdk&redirect_url=http%3A%2F%2F172.16.3.162%3A18502%2Faccount%2Fdirect-access";
        String url2 = "http://10.53.100.195:9800/bserver/yang/oauth/signCheck?code=xQpnrrLvuNmXPlBtSrdCZkBWAxcixuPd&client_id=72tBHkKQQn1zE5TN&timestamp=1671067841935&sign=90F43B6D519D40DE0E048C9D5137CD877B5009DA8C231BFE07E5AF9E92DF92B1&redirect_url=http%3A%2F%2F10.53.100.195%3A9800%2Fbserver%2Fyang%2Foauth%2FsignCheck";
        Map<String,String> map = getParameter(url);
        System.out.println(map);

        Map<String,String> map2 = getParameter(url2);
        System.out.println(map2);

        String url3 = "http://58.240.254.232:18501/openapi/oauth2/userinfo?access_token=cRakRdpYPc8xpLRKPZZGFNP4ed1tf8jw&signReq=3B6246E2E45F3F956B4375DBD1AACC49E24DF3F7DD7F1EB67743D358B8A5B279&appKey=9NALNeobt4l7nAvY&signMethod=HmacSHA256&signVersion=1&format=JSON&t=2022-12-15 10:55:51&v=1&nonce=335353366";
        String all = url3.replaceAll(" ", "%20");
        String encode = URLEncoder.encode(url3, "UTF-8");
        System.out.println(encode);
        System.out.println(all);
    }


    /**
     * 获得url中参数
     * @param url
     * @return map
     */
    public static Map<String,String> getParameter (String url){
        Map<String,String> map = new HashMap<String,String>();
        try {
            final String charset = "utf-8";
            url = URLDecoder.decode(url, charset);
            if (url.indexOf('?') != -1) {
                final String contents = url.substring(url.indexOf('?') + 1);
                String[] keyValues = contents.split("&");
                for (String keyValue : keyValues) {
                    String key = keyValue.substring(0, keyValue.indexOf("="));
                    String value = keyValue.substring(keyValue.indexOf("=") + 1);
                    map.put(key, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


}
